package com.skyline.wizard.searcher.impl;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.PostConstruct;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import com.skyline.common.bean.Page;
import com.skyline.user.model.User;
import com.skyline.user.type.Sex;
import com.skyline.wizard.mapper.UserObjMapper;
import com.skyline.wizard.searcher.UserSearcher;

@Component("userSearcher")
public class UserSearcherImpl extends BaseSearcherImpl<User> implements UserSearcher {

	private final static String COLUMN_BIRTHDAY = "birthday";
	private final static String COLUMN_CITY = "city";
	private final static String COLUMN_GRADUATE_SCHOOL = "graduateSchool";
	private final static String COLUMN_NAME = "name";
	private final static String COLUMN_NICKNAME = "nickname";
	private final static String COLUMN_PROVINCE = "province";
	private final static String COLUMN_SEX = "sex";
	
	private UserObjMapper mapper = null;

	private static DateFormat format = new SimpleDateFormat("yyyy-MM-dd");

	@Value("${wizard.userIndexPath}")
	private String indexPath;

	@PostConstruct
	public void inital() {
		mapper = UserObjMapper.getMapper();
		String[] fields = { COLUMN_GRADUATE_SCHOOL, COLUMN_NAME, COLUMN_NICKNAME };
		super.inital(indexPath, mapper, fields);
	}

	@Override
	public List<User> searchWithStandardAnalyzer(String name, String nickname, String province, String city,
			String graduateSchool, Sex sex, Integer lowAge, Integer upAge, Page page) {
		/*String[] shouldTermKeys = { COLUMN_NICKNAME, COLUMN_NAME };
		String[] shouldTermValues = { nickname, name };

		String[] mustTermKeys = { COLUMN_PROVINCE, COLUMN_CITY, COLUMN_GRADUATE_SCHOOL, COLUMN_SEX };
		String[] mustTermValues = { province, city, graduateSchool, sex.toString() };

		String[] mustNotTermKeys = {};
		String[] mustNotTermValues = {};

		String[] rangeKeys = { COLUMN_BIRTHDAY };

		Date d = new Date();
		d.setYear(d.getYear() - lowAge);
		String[] rangeEndValues = { format.format(d) };
		d = new Date();
		d.setYear(d.getYear() - upAge);
		String[] rangeBeginValues = { format.format(d) };

		return super.booleanSearchWithStandardAnalyzer(shouldTermKeys, shouldTermValues, mustTermKeys, mustTermValues,
				mustNotTermKeys, mustNotTermValues, rangeKeys, rangeBeginValues, rangeEndValues, page);*/
		return null;
	}

	@SuppressWarnings("deprecation")
	@Override
	public List<User> searchWithDefaultAnalyzer(String name, String nickname, String province, String city,
			String graduateSchool, Sex sex, Integer lowAge, Integer upAge, Page page) {

		List<String> shouldTermKeys=new ArrayList<String>();
		shouldTermKeys.add(COLUMN_NAME);
		List<String> shouldTermValues=new ArrayList<String>();
		shouldTermValues.add(name);

		List<String> mustTermKeys=new ArrayList<String>();
		List<String> mustTermValues=new ArrayList<String>();
		mustTermKeys.add(COLUMN_NICKNAME);
		mustTermValues.add(nickname);
		if(province!=null){
			mustTermKeys.add(COLUMN_PROVINCE);
			mustTermValues.add(province);
		}
		if(city!=null){
			mustTermKeys.add(COLUMN_CITY);
			mustTermValues.add(city);
		}
		if(graduateSchool!=null){
			mustTermKeys.add(COLUMN_GRADUATE_SCHOOL);
			mustTermValues.add(graduateSchool);
		}
		if(sex!=null){
			mustTermKeys.add(COLUMN_SEX);
			mustTermValues.add(sex.toString());
		}

		List<String> mustNotTermKeys=new ArrayList<String>();
		List<String> mustNotTermValues=new ArrayList<String>();

		List<String> rangeKeys = new ArrayList<String>();
		List<String> rangeEndValues =new ArrayList<String>();
		List<String> rangeBeginValues =new ArrayList<String>(); 
		if(lowAge!=null&&upAge!=null)
		{
			rangeKeys.add(COLUMN_BIRTHDAY);
		Date d = new Date();
		d.setYear(d.getYear() - lowAge);
		rangeEndValues.add( format.format(d));
		d = new Date();
		d.setYear(d.getYear() - upAge);
		rangeBeginValues.add(format.format(d));
		}
		

		return super.booleanSearchWithDefaultAnalyzer(shouldTermKeys, shouldTermValues, mustTermKeys, mustTermValues,
				mustNotTermKeys, mustNotTermValues, rangeKeys, rangeBeginValues, rangeEndValues, page);
	}

}
